


class Solution:

    def spiralOrder(self, matrix):

        l, r, t, b = 0, len(matrix[0]) - 1, 0, len(matrix) - 1

        ans = []
        while(l <= r and t <= b):
            if l < r and t <= b:
                for i in range(l, r + 1):
                    ans.append(matrix[t][i])
                t += 1
            if t <= b and l <= r:
                for j in range(t, b + 1):
                    ans.append(matrix[j][r])
                r -= 1
            if l <= r and t <= b:
                for k in range(r, l - 1, -1):
                    # print("----")
                    ans.append(matrix[b][k])
                
                b -= 1
        
            if t <= b and l<= r:
                for n in range(b, t - 1, -1):
                    print("----")
                    ans.append(matrix[n][l])
                l += 1
        return ans
    

if __name__ == "__main__":

    matrix = [[1,2,3],[4,5,6],[7,8,9]]
    matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
    matrix = [[1]]
    sol = Solution()
    ans = sol.spiralOrder(matrix)
    print(ans)     



